package Autodesk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import org.apache.hadoop.io.Text;

public class ReduceHours {
	
	public class HourComparator implements Comparator<String> {
	    @Override
	    public int compare(String o1, String o2) {
	    	int result;
	    	//String line1[]=o1.split("\t");
	    	//String line2[]=o2.split("\t");
	    	
	     //   String time1[]=line1[0].split(":");
	     //   String time2[]=line2[0].split(":");
	        
	        String time1[]=o1.split(":");
	        String time2[]=o2.split(":");
	        result=compareStrs(time1[0], time2[0]);//COMPARE HOUR
	        if(result==0){
	        	result=compareStrs(time1[1], time2[1]);//COMPARE MINUTES
	        	if (result==0){
	        		result=compareStrs(time1[2],time2[2]);//COMPARE SECONDS
	        	}
	        }
	    	return result;
	    }

	    /*
	     * If the result is 1 it means that time1>time2. On the contrary, if it returns -1 then time2 >time1
	     * If it returns 0, it means that they are equals.im
	     */

	}
	private int compareStrs(String time1, String time2) { 
		int result;
		if (Integer.parseInt(time1)>Integer.parseInt(time2)){
        	result=1;
        }else if(Integer.parseInt(time1)<Integer.parseInt(time2)){
        	result=-1;
        }else{
        	result=0;
        }
        return result;
	}
	
	/*
	 * The list has to be ordered by ascendant criteria 
	 */
	public boolean isSameSessionbyHour(String time1,String time2,int intervHour){
		boolean isTheSame=false;
        String t1[]=time1.split(":");
        String t2[]=time2.split(":");
		if(Integer.parseInt(t2[0])-Integer.parseInt(t1[0])==intervHour){
			int compareMinutes=this.compareStrs(t1[1], t2[1]);
			//System.out.println("compara"+t1[1]+"con"+t1[1]);
			if(compareMinutes>0){
				isTheSame=true;
			}else if(compareMinutes==0){
				int compareSeconds=this.compareStrs(t1[2], t2[2]);
				if(compareSeconds>=0){
					isTheSame=true;
				}
			}
			
		}else if(Integer.parseInt(t2[0])-Integer.parseInt(t1[0])<intervHour){//IS THE SAME HOUR 
			isTheSame=true;
		}
        return isTheSame;
		
	}
	
	private int getAmountSession(ArrayList<String> listHours){
		int ses=1;
		HourComparator hcomp=new HourComparator();
		for (int i=1; i<listHours.size();++i){
			if(!this.isSameSessionbyHour(listHours.get(i-1), listHours.get(i), 1)){
				ses=ses+1;
			}
		}
		return ses;
	}
	public void orderList(ArrayList<String> hourlist){
		
		Collections.sort((hourlist), new HourComparator());
	}
	
	
	public ArrayList<String> makeList(Iterator<Text> values){
		ArrayList<String> result= new ArrayList<String> ();
		while(values.hasNext()){
			result.add(values.next().toString());
		}
	
		return result;
	}
	public ArrayList<String> generateListHours(ArrayList<String> values){
	    ArrayList<String> list = new ArrayList<String>();
	    for(int i=0;i<values.size();i++){
	    	String line1[]=values.get(i).toString().split("\t");
	        list.add(line1[0]);
	    }
	    return list;
	}
	
	public void orderdIterable(){
		
	}

	private String generateFinish(String time){
        String result=null;
		String t1[]=time.split(":");
        int newHour=Integer.parseInt(t1[0]);
        if(newHour!=23){
        	newHour=newHour+1;
        	result=Integer.toString(newHour)+":"+t1[1]+":"+t1[2];
        }else{
        	result="23:59:59";
        }
        
        return result;
		
	}
	
	public ArrayList<ArrayList<String>> buildListOfSessions(ArrayList<String> listHours) {
		ArrayList<ArrayList<String>> listResult=new ArrayList<ArrayList<String>>();

		//ArrayList<String[]> listResult=new ArrayList<String[]>();
		String start=listHours.get(0);
		String finish=generateFinish(start);
		for(int i=1;i<listHours.size();i++){
			//System.out.println("COMPARE: "+listHours.get(i)+" WITH "+start);
			if(!isSameSessionbyHour(listHours.get(i-1),listHours.get(i), 1)){
				//System.out.println("New Session! Saved "+start+" and "+finish);
				ArrayList<String> newSession= new ArrayList<String>();
				newSession.add(start);
				newSession.add(finish);
				listResult.add(newSession);
				start=listHours.get(i);
			}
				finish=generateFinish(listHours.get(i));
		}
		ArrayList<String> newSession= new ArrayList<String>();
		newSession.add(start);
		newSession.add(finish);
		listResult.add(newSession);
		//System.out.println("New Session! Saved "+start+" and "+finish);
		// TODO Auto-generated method stub
		return listResult;
	}

	public ArrayList<String> identifySession(ArrayList<ArrayList<String>> listSessions,
			String hour) {
		ArrayList<String> sessionResult=new ArrayList<String>();
		HourComparator hcomp=new HourComparator();
		boolean foundSession=false;
		for(int i=0;i<listSessions.size();i++){
			ArrayList<String> session=listSessions.get(i);
			if(hcomp.compare(session.get(0), hour)<=0 && hcomp.compare(session.get(1), hour)>=0 ){
				sessionResult=session;
				//foundSession=true;
			}
		}
		//System.out.println("RESULT: "+sessionResult);
		return sessionResult;
	}
	
	
}
